//
//  LWStatusOrginalView.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/23.
//  Copyright © 2016年 Linwang. All rights reserved.
//


import UIKit
import SDWebImage
// LWStatusOrginalView原创微博视图
class LWStatusOrginalView: UIView {

    
    //  记录原创微博内容底部约束
    var originViewBottomConstraint: Constraint?

    
    // MARK: - 给原创里面绑定数据
    var statusViewModel:LWStatusViewModel?{
        didSet{
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            if let userImageUrl = statusViewModel?.status?.user?.profile_image_url {
                
                //使用SDWebImage加载图片

                userImageView.sd_setImage(with: URL(string: userImageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
                
            }
            
            timeLabel.text = statusViewModel?.timeContent
            
            sourceLabel.text = statusViewModel?.sourceContent
            
            contentLabel.text = statusViewModel?.status?.text
            
            
            membershipImageView.image = statusViewModel?.mbrankImage
            
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage

            
            
            //  卸载上次约束
            originViewBottomConstraint?.uninstall()
            
            //  判断原创微博的配图个数是否大于0
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  表示有配图
                
                //  显示配图, 修改约束, 绑定数据
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    originViewBottomConstraint = make.bottom.equalTo(pictureView).offset(LWHomeTabCellMargin).constraint
                })
                //  绑定数据源
                pictureView.picUrls = picUrls
            } else {
                //  表示没有配图
                //  隐藏配图, 修改约束
                pictureView.isHidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    originViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(LWHomeTabCellMargin).constraint
                })
                
                
                
            }

        }
    }
    
    // MARK: -  懒加载控件
    //用户头像
    private lazy var userImageView :UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证等级类型
    private lazy var verifiedTypeImageView :UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
//    昵称
    private lazy var screenNameLabel :UILabel = {
       
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 14)
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
        label.text = "黄林旺"
        return label
    }()
    //  配图
    private lazy var pictureView: LWStatusPictureView = {
        let view = LWStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
        
    }()

    //会员等级图片
    private lazy var membershipImageView :UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 时间
    private lazy var timeLabel :UILabel = {
        
        let label = UILabel(textColor:UIColor.orange,fontSize: 11)
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        label.text = "2016年09月23日15:06:41"
        label.text = "刚才"
        return label
    }()
    
    //来源
    private lazy var sourceLabel :UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGray,fontSize:11)
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
        label.text = "网易新闻"
        return label
    }()
    
    //微博内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.darkText, fontSize: 14)
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        label.text = "网易新闻网易新闻网易新闻网易新闻网易新闻网易新闻网易新闻网易新闻网易新闻"
        label.numberOfLines = 0
        return label
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件
    private func setupUI(){
        
        //添加子控件
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)

        // MARK: - 约束
        userImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(LWHomeTabCellMargin)
            make.leading.equalTo(self).offset(LWHomeTabCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) in
        
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(LWHomeTabCellMargin)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(LWHomeTabCellMargin)
        }
        
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(LWHomeTabCellMargin)
            make.bottom.equalTo(userImageView)
        }
       
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(LWHomeTabCellMargin)
            make.bottom.equalTo(timeLabel)
        }

        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView.snp_bottom).offset(LWHomeTabCellMargin)
            make.leading.equalTo(userImageView)
            make.width.equalTo( ScreenWidth -  2 * LWHomeTabCellMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(LWHomeTabCellMargin)
            make.leading.equalTo(contentLabel)
        }

        
        //关键约束
        self.snp_makeConstraints { (make) in
            originViewBottomConstraint = make.bottom.equalTo(pictureView).offset(LWHomeTabCellMargin).constraint

        }
    }

}
